// ActionScript file
import Modules.Dashboard.Dashboard;
import Modules.NavigationInfo;
import Modules.Windows.AdminWindow;
import Modules.Windows.CMDWindow;
import Modules.Windows.ReportsWindow;
import Modules.Windows.SalesDashboardWindow;
import Modules.Windows.SecurityWindow;

import com.desidea.common.CommonClass;
import com.desidea.common.IModuleWindow;
import com.desidea.common.MenuConstants;
import com.desidea.utils.DirtyChecker;
import com.desidea.utils.IDirtyChecker;

import flash.display.DisplayObject;
import flash.events.MouseEvent;

import mx.collections.ArrayCollection;
import mx.containers.Canvas;
import mx.containers.VBox;
import mx.controls.Alert;
import mx.controls.Button;
import mx.core.Application;

private var _tabButtons:ArrayCollection;
private var _leftButtons:ArrayCollection;

private function creationCompleted():void{

	var obj:Object = new Object();
	obj.ni = null;
	
	Application.application.data = obj;

	createTopNavigation();
	displayModule();
	
	this.txtWelcome.text = "Welcome : " + CommonClass.UserName;
	
}

private function processToolbarClick(reqMode:String):void{
	Application.application.data.ni = null;
	switch(reqMode){
		case "home":
			displayDashboard();
			break;
		case "logout":
			Application.application.DoSignOut();
			break;
		default:
			Alert.show("Yet To Implement");
			break;
	}
}

private function createTopNavigation():void{
	var bandList:XMLList = leftMenuXmlList.OuterMenu as XMLList;
	_leftButtons = new ArrayCollection();
	_tabButtons = new ArrayCollection();
	for each(var item:XML in bandList){
		
		if( item.@visible == "0") continue;
		
		if( item.@id == MenuConstants.ADMINISTRATION && !CommonClass.UserLoginInfo.IsSuperUser) continue;

		var cv:Canvas = new Canvas();
		cv.percentHeight = 100;
		cv.percentWidth = 100;
		cv.useHandCursor = true;
		cv.horizontalScrollPolicy = "off";
		
		cv.addChild( createModuleLinks(item.InnerMenu as XMLList, item.@label));

		var tabBtn:Button = this.createTabs(item, cv);

		this.leftNavVS.addChild(cv);
		this.leftNavVB.addChild(tabBtn);
	}

	this.leftNavVS.selectedIndex = 0;
	(this._tabButtons.getItemAt(0) as Button).styleName = "topSelectedBtn";
}

private function createTabs(item:XML, links:Canvas):Button{
	var btn:Button = new Button();
	btn.label = item.@label;
	btn.height = 30;
	btn.styleName = "topBtn";
	btn.setStyle("roundedBottomCorners","false");
	btn.addEventListener(MouseEvent.CLICK, onTabClick);

	var info:Object = new Object();
	info.id = item.@id;
	info.raw = item;
	info.canvas = links;
	
	btn.data = info;
	
	_tabButtons.addItem(btn);
	return btn;
}


public function doNavigate(ni:NavigationInfo):void{
	
	Application.application.data.ni = ni;
	
	var selectedTab:Button = null;
	
	for each(var btn:Button in _tabButtons){
		if(btn.data.id == ni.DestinationTab){
			selectedTab = btn;
//			selectedTab.data.ni = ni;
			break;
		}
	}
	
	if(selectedTab != null){
		selectedTab.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
	}

}

private function onTabClick(event:MouseEvent):void{
	var ni:NavigationInfo = Application.application.data.ni;
	var tabBtn:Button = event.currentTarget as Button;
	
	this.leftNavVS.selectedChild = tabBtn.data.canvas as Canvas;
	resetTabButtonStyle();
	(event.currentTarget as Button).styleName = "topSelectedBtn";
	
	if(ni!=null){
		var selectedLeftBtn:Button = null;
		
		for each(var btn:Button in _leftButtons){
			if(btn.data.id == ni.DestinationMenu){
				selectedLeftBtn = btn;
//				selectedLeftBtn.data.ni = (tabBtn.data.ni as NavigationInfo);
				break;
			}
		}
		
		if(selectedLeftBtn != null){
			selectedLeftBtn.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
		}
	}
	tabBtn.data.ni = null;
}

private function createModuleLinks(buttonList:XMLList, parentLabel:String=""):VBox{
	var vBox:VBox = new VBox();
	vBox.percentHeight = 100;
	vBox.percentWidth = 100;
	
	vBox.setStyle("paddingTop","0");
	vBox.setStyle("verticalGap","0");
	vBox.setStyle("horizontalAlign","left");
	
	for each(var bt:XML in buttonList){
		if( bt.@visible == "0") continue;

		var btn:Button = new Button();
		btn.styleName = "leftNavBtn";
		btn.setStyle("cornerRadius","0");
		btn.setStyle("textAlign","left");

		var info:Object = new Object();
		info.id = bt.@id;
		info.parentLabel = parentLabel;
		info.label = bt.@label;
		btn.data = info;
		btn.label = bt.@label;
		btn.toolTip = bt.@label;
		btn.percentWidth = 100; 
	
//		btn.setStyle("styleName","Link");   //added by Bhupesh, some colors are different than the Normal Button

		btn.addEventListener(MouseEvent.CLICK, leftMenuButtonClick);
		vBox.addChild(btn);	
		_leftButtons.addItem(btn);
	}
	
	return vBox;
}


public function leftMenuButtonClick(event:MouseEvent):void{
	
	// CODE FOR DIRTY CHECK
	if( DirtyChecker.getInstance().isDirty){
		if (DirtyChecker.getInstance().sourceContainer is IDirtyChecker){
			(DirtyChecker.getInstance().sourceContainer as IDirtyChecker).promptToSave();
		}else{
			Alert.show("Save & Proceed");
		}
		return;
	}
	
	var obj:Object = (event.currentTarget as Button).data;
	CommonClass.SelectedMenuCode = (obj.id).toString();
	displayModule(obj.id, obj, (event.currentTarget as Button));
}

private function displayModule(moduleCode:String = "", info:Object=null, sourceBtn:Button = null):void{
		resetLeftButtonStyle();
		
		var moduleWin:IModuleWindow;
		
		switch(moduleCode){
			case MenuConstants.DASHBOARD:
				displayDashboard();
				break;
			case MenuConstants.MY_ACCOUNT:
			case MenuConstants.MY_ACTIVITIES:
			case MenuConstants.MY_CONTACTS:
			case MenuConstants.MY_OPPORTUNITIES:
			case MenuConstants.MY_TASKS:
			case MenuConstants.BASIC_SEARCH:
				moduleWin = displayCMDInfo(info);
				break;
			case MenuConstants.OPPORTUNITY_PIPELINE:
				moduleWin = displaySalesDashboardInfo(info);
				break;
			case MenuConstants.MONTHLY_REPORT:
			case MenuConstants.WEEKLY_REPORT:
				moduleWin = displayReportInfo(info);
				break;

			case MenuConstants.PARENT_ACCOUNT_SUMMARY:
			case MenuConstants.RESELLER_ACCOUNT:
			case MenuConstants.PRODUCTS_PRICES:
			case MenuConstants.USERS:	
				moduleWin = displayAdminInfo(info);
				break;
			default:
				displayDashboard();
		}
		
		if(moduleWin == null){
			displayDashboard();
		}else{
			this.bodyCvs.addChild(moduleWin as DisplayObject);
		}
		
		if(sourceBtn != null)
			sourceBtn.styleName = "leftNavBtnSelected";

		Application.application.data.ni = null;
}

private function resetTabButtonStyle():void{
	for each(var btn:Button in _tabButtons){
		btn.styleName = "topBtn";
	}
}

private function resetLeftButtonStyle():void{
	for each(var btn:Button in _leftButtons){
		btn.styleName = "leftNavBtn";
	}
}

/**
 * 
 * */
private function displayDashboard():void{
	resetLeftButtonStyle();

	this.bodyCvs.removeAllChildren();
	var dashboard:Dashboard = new Dashboard();
	this.txtModuleHeading.text = "My CMD Site: Home";
	this.bodyCvs.addChild(dashboard);
}

/**
 * 
 * */
private function displayCMDInfo(info:Object):IModuleWindow{
	this.bodyCvs.removeAllChildren();
	var win:CMDWindow = new CMDWindow();
	this.txtModuleHeading.text = info.parentLabel + " : " + info.label;
	win.data = info;
	win.NavInfo = Application.application.data.ni;
	return win;
}

/**
 * 
 * */
private function displaySalesDashboardInfo(info:Object):IModuleWindow{
	this.bodyCvs.removeAllChildren();
	var win:SalesDashboardWindow = new SalesDashboardWindow();
	this.txtModuleHeading.text = info.parentLabel + " : " + info.label;
	win.data = info;
	return win;
}

/**
 * 
 * */
private function displayReportInfo(info:Object):IModuleWindow{
	this.bodyCvs.removeAllChildren();
	var win:ReportsWindow = new ReportsWindow();
	this.txtModuleHeading.text = info.parentLabel + " : " + info.label;
	win.data = info;
	return win;
}

/**
 * 
 * */
private function displayAdminInfo(info:Object):IModuleWindow{
	this.bodyCvs.removeAllChildren();
	var win:AdminWindow = new AdminWindow();
	this.txtModuleHeading.text = info.parentLabel + " : " + info.label;
	win.data = info;
	return win;
}

/**
 * 
 * */
private function displaySecurityInfo(info:Object):IModuleWindow{
	this.bodyCvs.removeAllChildren();
	var win:SecurityWindow = new SecurityWindow();
	this.txtModuleHeading.text = info.parentLabel + " : " + info.label;
	win.data = info;
	return win;
}
